﻿@model Models.Users

@{
    Layout = null;
    ViewData["Title"] = "用户编辑";
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title>@ViewData["Title"]</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <environment include="Development">
        <link rel="stylesheet" href="~/layer/css/layui.css" media="all" />
        <link rel="stylesheet" href="~/layer/css/admin.css" media="all" />
        <style>
            select {
                height: 36px;
                -moz-appearance: none;
                -webkit-appearance: none;
                border-color: #e6e6e6;
                width: 100px;
                line-height: 25px;
                text-indent: 5px;
                background-color: transparent;
            }
        </style>
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="~/layer/css/layui.css" asp-append-version="true" media="all" />
        <link rel="stylesheet" href="~/layer/css/admin.css" asp-append-version="true" media="all" />
        <style>
            select {
                height: 36px;
                -moz-appearance: none;
                -webkit-appearance: none;
                border-color: #e6e6e6;
                width: 100px;
                line-height: 25px;
                text-indent: 5px;
                background-color: transparent;
            }
        </style>
    </environment>
</head>
<body id="iosiframe">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">用户编辑</div>
                    <div class="layui-card-body" pad15>
                        <form class="layui-form" action="" lay-filter="component-form-element">
                            @if (Model != null && Model.Id > 0) {
                                <input type="hidden" asp-for="Id" />
                                <input type="hidden" asp-for="SecondaryPwd" />
                                <input type="hidden" asp-for="Overage" />
                                <input type="hidden" asp-for="CreateTime" />
                                <input type="hidden" asp-for="TeamCount"  />
                                @if (Model.UserType == Code.UserTypes.普通用户) {
                                    <input type="hidden" asp-for="UserType" />
                                    <div class="layui-form-item">
                                        <div class="layui-col-lg6">
                                            <label class="layui-form-label">用户编号：</label>
                                            <div class="layui-input-block">
                                                <input type="text" asp-for="UserCode" maxlength="8" readonly lay-verify="required" placeholder="" autocomplete="off" class="layui-input" />
                                            </div>
                                        </div>
                                    </div>
                                } else if (Model.UserType == Code.UserTypes.超级管理员) {
                                    <input type="hidden" asp-for="UserType" value="@Code.UserTypes.超级管理员" />
                                }
                            } else if ((Code.UserTypes)ViewData["userType"] == Code.UserTypes.普通用户) {
                                <input type="hidden" asp-for="UserType" value="@Code.UserTypes.普通用户" />
                            }
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    @if ((Code.UserTypes)ViewData["userType"] == Code.UserTypes.管理员) {
                                        <label class="layui-form-label">账号：</label>
                                    } else {
                                        <label class="layui-form-label">姓名：</label>
                                    }
                                    <div class="layui-input-block">
                                        <input type="text" maxlength="20" asp-for="UserName" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <label class="layui-form-label">密码：</label>
                                    <div class="layui-input-block">
                                        @if (Model == null) {
                                            <input type="password" maxlength="18" asp-for="Password" placeholder="" lay-verify="required" autocomplete="off" class="layui-input">
                                            <input type="hidden" value="" id="hidPassword" />
                                        } else {
                                            <input type="password" maxlength="18" asp-for="Password" placeholder="" lay-verify="" autocomplete="off" class="layui-input">
                                            <input type="hidden" value="@Model.Password" id="hidPassword" />
                                        }
                                    </div>
                                </div>
                            </div>
                            @if ((Code.UserTypes)ViewData["userType"] == Code.UserTypes.普通用户) {
                                <div class="layui-form-item">
                                    <div class="layui-col-lg6">
                                        <label class="layui-form-label">联系手机：</label>
                                        <div class="layui-input-block">
                                            <input type="text" maxlength="18" asp-for="Phone" placeholder="" lay-verify="required|phone" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-col-lg6">
                                        <label class="layui-form-label">身份证：</label>
                                        <div class="layui-input-block">
                                            <input type="text" maxlength="18" asp-for="IdCard" placeholder="" lay-verify="required|identity" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">用户等级：</label>
                                    <div class="layui-input-block">
                                        <select asp-for="Level" lay-verify="required" lay-ignore lay-filter="level">
                                            @foreach (var m in (ViewData["level"] as List<Models.ItemList>)) {
                                                @if (Model != null) {
                                                    @if ((int)Model.Level == m.value) {
                                                        <option value="@m.value" selected>@m.text</option>
                                                    } else {
                                                        <option value="@m.value">@m.text</option>
                                                    }
                                                } else {
                                                    @if ((int)Code.LevelCode.未激活 == m.value) {
                                                        <option value="@m.value" selected>@m.text</option>
                                                    } else {
                                                        <option value="@m.value">@m.text</option>
                                                    }
                                                }
                                            }
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">代理省份：</label>
                                    <div class="layui-input-block">
                                        @if (Model != null) {
                                            @if (Model.Level == Code.LevelCode.全国总代 || Model.Level == Code.LevelCode.省代) {
                                                <select asp-for="ProvinceId" lay-ignore lay-verify="">
                                                    @foreach (var m in (ViewData["province"] as List<Models.Area>)) {
                                                        @if (Model.ProvinceId == m.id) {
                                                            <option value="@m.id" selected>@m.name</option>
                                                        } else {
                                                            <option value="@m.id">@m.name</option>
                                                        }}
                                                </select>
                                            } else {
                                                <select asp-for="ProvinceId" lay-ignore lay-verify="" disabled>
                                                    @foreach (var m in (ViewData["province"] as List<Models.Area>)) {
                                                        <option value="@m.id">@m.name</option>
                                                    }
                                                </select>

                                            }
                                        } else {
                                            <select asp-for="ProvinceId" lay-ignore lay-verify="" disabled>
                                                @foreach (var m in (ViewData["province"] as List<Models.Area>)) {
                                                    <option value="@m.id">@m.name</option>
                                                }
                                            </select>
                                        }
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">代理城市：</label>
                                    <div class="layui-input-block">
                                        <div class="layui-inline">
                                            @if (Model != null) {
                                                @if (Model.Level == Code.LevelCode.全国总代 || Model.Level == Code.LevelCode.省代 || Model.Level == Code.LevelCode.大区总代 || Model.Level == Code.LevelCode.市代) {
                                                    <select name="pro" id="ProId" lay-ignore lay-verify="" lay-filter="pro">
                                                        @foreach (var m in (ViewData["allPro"] as List<Models.Area>)) {
                                                            @if ((int)ViewData["pro"] == m.id) {
                                                                <option value="@m.id" selected>@m.name</option>
                                                            } else {
                                                                <option value="@m.id">@m.name</option>
                                                            }
                                                        }
                                                    </select>
                                                } else {
                                                    <select name="pro" id="ProId" lay-ignore lay-verify="" lay-filter="pro" disabled>
                                                        @foreach (var m in (ViewData["allPro"] as List<Models.Area>)) {
                                                            <option value="@m.id">@m.name</option>
                                                        }
                                                    </select>
                                                }
                                            } else {
                                                <select name="pro" id="ProId" lay-ignore lay-verify="" lay-filter="pro" disabled>
                                                    @foreach (var m in (ViewData["allPro"] as List<Models.Area>)) {
                                                        <option value="@m.id">@m.name</option>
                                                    }
                                                </select>
                                            }
                                        </div>
                                        <div class="layui-inline" id="divCity">
                                            @if (Model != null && Model.CityId > 0) {
                                                <select asp-for="CityId" lay-ignore lay-verify="">
                                                    @foreach (var m in (ViewData["city"] as List<Models.Area>)) {
                                                        @if (Model.CityId == m.id) {
                                                            <option value="@m.id" selected>@m.name</option>
                                                        } else {
                                                            <option value="@m.id">@m.name</option>
                                                        }}
                                                </select>
                                            }
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">推荐人：</label>
                                    <div class="layui-input-block">
                                        <select asp-for="ParentId" lay-ignore lay-verify="">
                                            @foreach (var m in (ViewData["users"] as List<Models.Users>)) {
                                                @if (Model != null) {
                                                    @if (Model.ParentId == m.Id) {
                                                        <option value="@m.Id" selected>@m.UserName</option>
                                                    } else {
                                                        <option value="@m.Id">@m.UserName</option>
                                                    }
                                                } else {
                                                    <option value="@m.Id">@m.UserName</option>
                                                }}
                                        </select>
                                    </div>
                                </div>
                            } else if ((Code.UserTypes)ViewData["userType"] == Code.UserTypes.管理员) {
                                if (ViewBag.Roles != null) {
                                    var roles = ViewBag.Roles as List<Models.RoleInfo>;
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">角色：</label>
                                        <div class="layui-input-block">
                                            <select asp-for="ParentId" lay-ignore lay-verify="">
                                                @foreach (var m in roles) {
                                                    @if (Model != null) {
                                                        @if (Model.ParentId == m.Id) {
                                                            <option value="@m.Id" selected>@m.Text</option>
                                                        } else {
                                                            <option value="@m.Id">@m.Text</option>
                                                        }
                                                    } else {
                                                        <option value="@m.Id">@m.Text</option>
                                                    }}
                                            </select>
                                        </div>
                                    </div>
                                }
                            }
                            <div class="layui-form-item">
                                <div class="layui-input-block">
                                    <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
                                    <a class="layui-btn  layui-btn-primary" lay-filter="component-form-element" href="@Url.Action("list","account",new { userTypes = ViewData["userType"]})">返回</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="~/layer/layui.js"></script>
    <script>
        layui.config({
            base: '../../layer/' //静态资源所在路径
        }).extend({
            index: 'index' //主入口模块
        }).use(['form', 'upload', 'layedit'], function () {
            var $ = layui.$
                , form = layui.form;

            var ua = navigator.userAgent.toLowerCase();
            var screenwidth = window.screen.width;
            if (!/iphone|ipad|ipod/.test(ua)) {
                $("#iosiframe").attr("scrolling", "auto");
            } else {
                $('#iosiframe').width(screenwidth + 'px');
            }

            form.render(null, 'component-form-element');

            //提交
            form.on('submit(component-form-element)', function (data) {
                var pwd = $('#hidPassword').val();

                if (pwd.length > 0 && data.field.Password.length == 0) {
                    data.field.Password = pwd;
                }

                if (data.field.ProvinceId && data.field.ProvinceId.length == 0) {
                    data.field.ProvinceId = 0
                }
                if (data.field.CityId && data.field.CityId.length == 0) {
                    data.field.CityId = 0
                }

                $.ajax({
                    url: '@Url.Action("saveuser", "api")',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify(data.field),
                    type:'post'
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    alert(errorThrown, { icon: 2, title: textStatus });
                }).done(function (res) {
                    layer.msg(res.message, { offset: '15px', icon: (res.code == 0 ? 1 : 2), time: 1000 }, function () {
                        if (res.code == 0) {
                            location.href = '@Url.Action("list", "account", new { userTypes = ViewData["userType"] })';
                        }
                    });
                });
                return false;
            });

            $('select[lay-filter="level"]').on('change', function () {
                var val = this.value;
                if (val == 1 || val == 2) {
                    $('#ProvinceId').removeAttr('disabled');
                    $('#ProId').removeAttr('disabled');
                } else if (val == 3 || val == 4) {
                    $('#ProvinceId').val(0);
                    $('#ProvinceId').attr('disabled', 'disabled');

                    $('#ProId').removeAttr('disabled');
                } else {
                    $('#ProvinceId').val(0);
                    $('#ProvinceId').attr('disabled', 'disabled');

                    $('#ProId').val(0);
                    $('#ProId').attr('disabled', 'disabled');

                    $('#divCity').html('');
                }
            });

            $('select[lay-filter="pro"]').on('change', function () {
                var val = this.value;
                $.ajax({
                    url: '@Url.Action("GetCityList", "api")?id=' + val +'&userId=' + '@if (Model != null) { @Model.Id }',
                    dataType: 'json',
                    contentType: 'application/json',
                    type:'get'
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    alert(errorThrown, { icon: 2, title: textStatus });
                }).done(function (res) {
                    var html = '';

                    if (res.data && res.data.length > 0) {
                        html = '<select name="CityId" id="CityId"  lay-ignore lay-verify="">';

                        res.data.forEach(function (item) {
                            if (item.id == 0) {
                                html += '<option value=' + item.id + ' selected>' + item.name + '</option>';
                            }
                            else {
                                html += '<option value=' + item.id + '>' + item.name + '</option>';
                            }
                        });

                        html += '</select>';
                    }

                    $('#divCity').html(html);
                });
            });

            @*form.on('select(level)', function (data) {
                if (data.value == 1 || data.value == 2) {
                    $('#ProvinceId').removeAttr('disabled');
                    $('#ProId').removeAttr('disabled');
                } else if (data.value == 3 || data.value == 4) {
                    $('#ProvinceId').val(0);
                    $('#ProvinceId').attr('disabled', 'disabled');

                    $('#ProId').removeAttr('disabled');
                } else {
                    $('#ProvinceId').val(0);
                    $('#ProvinceId').attr('disabled', 'disabled');

                    $('#ProId').val(0);
                    $('#ProId').attr('disabled', 'disabled');

                    $('#divCity').html('');
                }

                form.render('select');
            });

            form.on('select(pro)', function (data) {
                $.ajax({
                    url: '@Url.Action("GetCityList", "api")?id='+data.value+'&userId=' + '@if (Model != null) { @Model.Id }',
                    dataType: 'json',
                    contentType: 'application/json',
                    type:'get'
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    alert(errorThrown, { icon: 2, title: textStatus });
                }).done(function (res) {
                    var html = '';

                    if (res.data && res.data.length > 0) {
                        html = '<select name="CityId" id="CityId"  lay-ignore lay-verify="">';

                        res.data.forEach(function (item) {
                            if (item.id == 0) {
                                html += '<option value=' + item.id + ' selected>' + item.name + '</option>';
                            }
                            else {
                                html += '<option value=' + item.id + '>' + item.name + '</option>';
                            }
                        });

                        html += '</select>';
                    }

                    $('#divCity').html(html);

                    form.render('select');
                });
            });*@
        });

    </script>
</body>
</html>
